package com.salinity.kun.task;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import com.salinity.kun.helper.OracleHelper;

public class UpdateDatongFluxTask implements ITask {

	private Path inputPath;
	private String stationName;
	private SimpleDateFormat sdf = null;
	public UpdateDatongFluxTask(Path inputPath,String stationName) {
		this.inputPath = inputPath;
		this.stationName = stationName;
	}
	@Override
	public boolean run() throws IOException, ParseException {
		 
		try {
			doUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	
	public void doUpdate() throws IOException, SQLException {
		StringBuilder sb = new StringBuilder();
		String line = null;
		String[] strArray = null;
		StringBuilder sqlBuiler = new StringBuilder();
		Calendar calendar = Calendar.getInstance();
		double val = 0;
		Integer year = 0;
		String tableName = "T_SZHD_RGSWLLXX_NOW";
		sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		OracleHelper oracleHelper = new OracleHelper();
		
		Connection conn = oracleHelper.prepareConection();
		sqlBuiler.append("insert into ")
		         .append(tableName)
		         .append(" values( ?,'")
		         .append(stationName)
		         .append("',?,null,")
		         .append("to_date(?,'dd-mm-yyyy hh24:mi:ss'))");
		
		
		System.out.println(sqlBuiler.toString());
		PreparedStatement pre = conn.prepareStatement(sqlBuiler.toString());
		
		try(BufferedReader br = Files.newBufferedReader(inputPath)){
			while((line=br.readLine())!=null) {
				strArray = line.split(",");
				if(strArray.length==2) {
					 year = Integer.valueOf(strArray[1].trim());
					 calendar.set(year, 0, 1, 0, 0, 0);
					 continue;
				}		
				val = Double.parseDouble(strArray[0]);
				//Date plus
				writeToDB(pre,calendar.getTime(),val);
				calendar.add(Calendar.DAY_OF_YEAR, 1);
			}
		}
		System.out.println("写入完成，去除重复数据中...");
		OracleHelper.deduplication(conn, tableName);
		System.out.println("重复数据去除完成...");
		oracleHelper.end();
	}
	
	private boolean writeToDB(PreparedStatement pre,Date date,double value) throws SQLException {
		System.out.println(date.getTime()+"");
		pre.setString(1, sdf.format(date));
		pre.setDouble(2, value);;
		pre.setString(3, sdf.format(date));
		return pre.execute();
	}
	
	public static void main(String[] args) {
	
		Path path = Paths.get("E:\\Kuner\\研究生\\论文相关\\data\\流量\\DT_FLUX.csv");
		String stationName ="大通";
		UpdateDatongFluxTask updateTask = new UpdateDatongFluxTask(path, stationName);
		try {
			updateTask.run();
		} catch (IOException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
